﻿#include "formkeydata.h"
#include "ui_formkeydata.h"
#include <QMimeData>
#include <QDebug>
#include <QMessageBox>
#include <QRegularExpression>
#include <QDir>
#include <QTextCodec>


FormKeyData::FormKeyData(QWidget *parent) :
    QWidget(parent), ui(new Ui::FormKeyData)
{
    ui->setupUi(this);
}

FormKeyData::~FormKeyData()
{
    delete ui;
}

void FormKeyData::dragEnterEvent(QDragEnterEvent *event)
{
    event->acceptProposedAction();
}

void FormKeyData::dropEvent(QDropEvent *event)
{
    if(!v_data.empty()) v_data.clear();
    QString filePath = event->mimeData()->urls().first().toString();
    filePath.replace("file:///", "");
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QMessageBox::warning(this, "错误", "文件打开失败!");
        return;
    }
    QByteArray data = file.readAll();
    // 尝试自动识别编码
    QList<QByteArray> availableCodecs = QTextCodec::availableCodecs();
    QTextCodec *codec = nullptr;
    foreach (const QByteArray &codecName, availableCodecs) {
        QTextCodec *tempCodec = QTextCodec::codecForName(codecName);
        if (tempCodec) {
            QTextDecoder *decoder = tempCodec->makeDecoder();
            QString decoded = decoder->toUnicode(data);
            if (!decoded.isEmpty()) {
                codec = tempCodec;
                break;
            }
        }
    }
    file.seek(0);
    QTextStream in(&file);
    if (!codec) {
        if(QMessageBox::No==QMessageBox::warning(this, "错误", "无法识别文件编码,是否继续？", QMessageBox::Yes|QMessageBox::No))
            return;
    }else
        in.setCodec(codec);
    // 获取第一行
    QString line=in.readLine();
    if(!line.isEmpty()){
        QStringList v_buf = line.split(reg);
        if(!v_buf.isEmpty()){
            ui->comboBox->clear();
            ui->comboBox_2->clear();
            ui->comboBox->addItem("");
            ui->comboBox_2->addItem("");
            ui->comboBox->addItems(v_buf);
            ui->comboBox_2->addItems(v_buf);
            ui->comboBox->setCurrentIndex(1);
            v_data.push_back(v_buf);
        }
    }
    // 后面的
    while (!in.atEnd()) {
        line = in.readLine();
        QStringList v_buf = line.split(reg);
        if(!v_buf.isEmpty()){
            v_data.push_back(v_buf);
        }
    }
    file.close();
    ui->label_3->setText("总数:"+QString::number(v_data.size()));
}

// 保存并跳转
void FormKeyData::on_pushButton_clicked()
{
    if(ui->lineEdit_3->text().isEmpty()){
        QMessageBox::warning(this, "错误", "请输入工单号.");
        ui->lineEdit_3->setFocus();
        return;
    }
    QDir dir("./btw/"+ui->lineEdit_3->text());
    if(dir.exists()){
        if(QMessageBox::No==QMessageBox::warning(this, "错误", "工单号已存在,是否覆盖？", QMessageBox::Yes|QMessageBox::No)){
            ui->lineEdit_3->selectAll();
            ui->lineEdit_3->setFocus();
            return;
        }
    }else{
        if(!QDir().mkpath(dir.path())){
            QMessageBox::warning(this, "错误", "工单号文件夹创建失败!");
            ui->lineEdit_3->setFocus();
            return;
        }
    }
    int sn_index=ui->comboBox->currentIndex(), tusn_index=ui->comboBox_2->currentIndex();
    QString sn_field = ui->lineEdit->text(), tusn_field = ui->lineEdit_2->text();
    if(sn_index<1) return;
    // 保持到文件中
    QFile file(dir.path()+"/print_all_data.txt");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        QMessageBox::warning(this, "错误", "数据文件保存失败!");
        return ;
    }
    QTextStream out(&file);
    // 先保存字段
    out << sn_field << "," << tusn_field << "\n";
    // 保存所有数据
    for (int i=0; i<v_data.size(); i++) {
        out << v_data[i][sn_index-1] << ",";
        if(tusn_index>0) out << v_data[i][tusn_index-1];
        out << "\n";
    }
    file.close();
    QMessageBox::information(this, "成功", "数据文件保存成功!");
    emit switch_tab_on(dir.path());
}

void FormKeyData::on_lineEdit_3_returnPressed()
{
    on_pushButton_clicked();
}


void FormKeyData::on_comboBox_currentTextChanged(const QString &arg1)
{
    ui->label_4->setText("字符数:"+QString::number(arg1.size()));
}


void FormKeyData::on_comboBox_2_currentTextChanged(const QString &arg1)
{
    ui->label_5->setText("字符数:"+QString::number(arg1.size()));
}

